package com.mumu.muPictureBackend.Controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mumu.muPictureBackend.annotation.AuthCheck;
import com.mumu.muPictureBackend.common.BaseResponse;
import com.mumu.muPictureBackend.common.ResultUtils;
import com.mumu.muPictureBackend.constant.UserConstant;
import com.mumu.muPictureBackend.exception.BusinessException;
import com.mumu.muPictureBackend.exception.ErrorCode;
import com.mumu.muPictureBackend.exception.ThrowUtils;
import com.mumu.muPictureBackend.model.dto.user.*;
import com.mumu.muPictureBackend.model.entity.User;
import com.mumu.muPictureBackend.model.enums.UserRoleEnum;
import com.mumu.muPictureBackend.model.vo.UserVO;
import com.mumu.muPictureBackend.service.AdminService;
import com.mumu.muPictureBackend.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    AdminService adminService;

    /**
     * 用户注册请求
     */
    @PostMapping("/register")
//    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.NOT_FOUND_ERROR, "请输入账号和密码");
        return ResultUtils.success(userService.userRegister(userRegisterRequest));//这里ResultUtils.success会返回封装好后的BaseResponse对象
    }


    /**
     * 用户登录请求
     * 用POST:提交数据触发服务器状态更新(提交隐私信息之后让这个服务器进入新状态(可以访问更多请求的新状态))
     * 返回值类型就用Long 返回该登录用户的id
     */
    @PostMapping("/login")
    public BaseResponse<User> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.NOT_FOUND_ERROR, "请输入账号和密码");
        return ResultUtils.success(userService.userLogin(userLoginRequest, request));
    }

    /**
     * 获取登录用户
     *
     * @param request 登录的时候将用户信息存储到了request的Session中 这里需要获取一下
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<User> getLogin(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return ResultUtils.success(user);
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> logout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.userLogout(request));
    }

    /**
     * 根据id获取用户
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<User> getUser(Long id) {
        return ResultUtils.success(adminService.getUserById(id));
    }

    @GetMapping("/get/vo")
    @AuthCheck(mustRole = UserRoleEnum.USER)
    public BaseResponse<UserVO> getUserVOById(Long id) {
        return ResultUtils.success(userService.getUserById(id));
    }


    /**
     * User权限 获取userlist集合 UserVO 脱敏后的 这里用post是因为这里用的是复杂的分页查询 需要根据条件查询 并且用post封装数据会更容易一点 所以这么复杂的查询就直接用post即可
     *
     * @param queryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<Page<UserVO>> getUserVOLIst(@RequestBody UserQueryRequest queryRequest) {
        ThrowUtils.throwIf(queryRequest == null, ErrorCode.NOT_FOUND_ERROR, "请求参数呢");
        int current = queryRequest.getCurrent();
        int pageSize = queryRequest.getPageSize();
        //根据条件,分页参数查询数据 返回的是User的Page集合 但是我需要UserVO的集合 所以下面再new一个Page<UserVO>
        Page<User> userPage = adminService.page(new Page<>(current, pageSize), adminService.getQueryWrapper(queryRequest));
        Page<UserVO> userVOPage = new Page<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());
        //将UserLIst转换成UserVOList再存入Page<UserVO>中
        List<UserVO> userVOList = userService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVOList);
        //返回page里面有数据,当前页数,每页显示的记录数,返回的总记录数
        return ResultUtils.success(userVOPage);
    }


    /**
     * 创建用户对象
     *
     * @param userCreateRequest
     * @return
     */
    @PostMapping("/add")//创建一个已有的资源就用post
    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<Long> createUser(@RequestBody UserCreateRequest userCreateRequest) {
        return ResultUtils.success(adminService.createUser(userCreateRequest));
    }


    @PostMapping("/delete")
    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> deleteUser(
            @RequestBody UserDeleteRequest userDeleteRequest
    ) {
        Long id=userDeleteRequest.getId();
        return ResultUtils.success(adminService.deleteById(id));
    }

    /**
     * 更新用户
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> updateUser(UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null, ErrorCode.NOT_FOUND_ERROR);
        User user = new User();
        BeanUtil.copyProperties(userUpdateRequest, user);
        ThrowUtils.throwIf(!adminService.updateById(user), ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

}
